﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EDAC_AFS_Lib
{
    public class EDAC_TGID
    {
        public static Exception ExAfsStringInvFormat = new Exception(@"Invalid Input Format.
The format of an Agency Fleet Subfleet string must be '##-###' where # represent a numeric value,
OR 'XXX' where X represent and hexadecimal character.");

        private int _TgId = 0;
        private int _Agency = 0;
        private int _Fleet = 0;
        private int _SubFleet = 0;

        public int TgId
        {
            get { return _TgId; }
            set 
            { 
                _TgId = value;
                BitArray bits = new BitArray(new int[] { _TgId });

                BitArray Agency_ba = new BitArray(sizeof(int)*8);
                BitArray Fleet_ba = new BitArray(sizeof(int)*8);
                BitArray SubFleet_ba = new BitArray(sizeof(int)*8);

                int idx = 0;
                foreach (bool bit in bits)
                {
                    if (idx < 3) SubFleet_ba[idx] = bit;
                    else if (idx < 7) Fleet_ba[idx - 3] = bit;
                    else if (idx < 11) Agency_ba[idx - 7] = bit;
                    
                    
                    
                    if(idx >= 11) break;
                    idx++;
                }
                int[] tmp = new int[1]; ;

                SubFleet_ba.CopyTo(tmp, 0);
                _SubFleet = tmp[0];

                Fleet_ba.CopyTo(tmp, 0);
                _Fleet = tmp[0];
                Agency_ba.CopyTo(tmp, 0);
                _Agency = tmp[0];
            }

        }

        public int Agency
        {
            get { return _Agency; }
            set
            {
                _Agency = value;
                setFromAfs();
            }
        }
        public int Fleet
        {
            get { return _Fleet; }
            set
            {
                _Fleet = value;
                setFromAfs();
            }
        }
        public int SubFleet
        {
            get { return _SubFleet; }
            set
            {
                _SubFleet = value;
                setFromAfs();
            }
        }

        private void setFromAfs()
        {
            BitArray Agency_ba = new BitArray(new int[] { _Agency });
            BitArray Fleet_ba = new BitArray(new int[] { _Fleet });
            BitArray SubFleet_ba = new BitArray(new int[] { _SubFleet });

            BitArray AfsDec_ba = new BitArray(sizeof(int)*8);

            for (int i = 0; i < 11; i++)
            {
                if (i < 3) AfsDec_ba[i] = SubFleet_ba[i];
                else if (i < 7) AfsDec_ba[i] = Fleet_ba[i - 3];
                else AfsDec_ba[i] = Agency_ba[i - 7];
            }

            int[] tmp = new int[1];
            AfsDec_ba.CopyTo(tmp, 0);
            _TgId = tmp[0];
        }
        
        /// <summary>
        /// Get the TgId as a string
        /// </summary>
        /// <returns>return the TgId as a string in 00-000 Agency Fleet SubFleet format.</returns>
        public override string ToString()
        {
            return ToString("A");
        }

        /// <summary>
        /// Get the TgId as a string
        /// </summary>
        /// <param name="FormatProvider">
        /// A = 00-000 Agency Fleet SubFleet format.
        /// D = 000 Decimal format.
        /// X = XXX Hexadecimal format.</param>
        /// <returns>return the TgId as a string in the Decimal, Afs or Hexadecimal format</returns>
        public string ToString(string FormatProvider)
        {
            switch (FormatProvider)
            {
                case "A":
                    return this.Agency.ToString("00") + "-" + this.Fleet.ToString("00") + this.SubFleet.ToString("0");
                case "D":
                    return this.TgId.ToString("000");
                case "X":
                    return this.TgId.ToString("X3");

            }
            return "ERROR";
        }

        public EDAC_TGID()
        {


        }

        public EDAC_TGID(int tgid)
        {
            TgId = tgid;
        }

        public EDAC_TGID(int agency, int fleet, int subFleet)
        {
            Agency = agency;
            Fleet = fleet;
            SubFleet = subFleet;
        }

        public EDAC_TGID(string AFS_TgId)
        {
            if (AFS_TgId.Contains('-'))//anticipating 00-000 format AFS String
            {
                string[] s = AFS_TgId.Split(new char[] { '-' });
                if (s.Length != 2) throw ExAfsStringInvFormat;
                if (s[0].Length != 2 || s[1].Length != 3) throw ExAfsStringInvFormat;

                Agency = Convert.ToInt32(s[0]);
                Fleet = Convert.ToInt32(s[1].Substring(0, 2));
                SubFleet = Convert.ToInt32(s[1].Substring(2));
            }
            else//anticipating XXX hexadecimal AFS format
            {
                if (AFS_TgId.Length != 3) throw ExAfsStringInvFormat;
                TgId = Convert.ToInt32(AFS_TgId, 16);
            }
        }

    }


}
